package com.moyu.user.service.sysuser.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.github.pagehelper.PageHelper;
import com.moyu.common.core.constants.Constants;
import com.moyu.common.core.enums.ResultCode;
import com.moyu.common.core.utils.BCryptUtils;
import com.moyu.common.core.utils.BuildAccount;
import com.moyu.user.domain.user.dto.UserDTO;
import com.moyu.user.domain.user.dto.UserQueryDTO;
import com.moyu.user.domain.user.vo.UserVO;
import com.moyu.common.security.exception.ServiceException;
import com.moyu.user.domain.sysuser.SysUser;
import com.moyu.user.domain.sysuser.dto.SysUserSaveDTO;
import com.moyu.user.domain.user.User;
import com.moyu.user.mapper.sysuser.SysUserMapper;
import com.moyu.user.mapper.user.UserMapper;
import com.moyu.user.service.sysuser.ISysUserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * 系统管理员服务层
 *
 * @Author 赵彦博
 * @Date 2024/12/20 17:08
 */
@Slf4j
@Service
public class SysUserServiceImpl implements ISysUserService {

    @Autowired
    private SysUserMapper sysUserMapper;

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private BuildAccount account;

    @Override
    public int add(SysUserSaveDTO sysUserSaveDTO) {
        //将dto转为实体
        List<SysUser> sysUserList = new ArrayList<>();
        SysUser sysUser = new SysUser();
        try {
            sysUserList = sysUserMapper.selectList(new LambdaQueryWrapper<SysUser>()
                    .eq(SysUser::getUsername, sysUserSaveDTO.getUsername()));
        } catch (Exception e) {
            log.error("执行sql-查询出错:" +e.getCause());
        }
        if (CollectionUtil.isNotEmpty(sysUserList)) {
            //用户已经存在
            //自定义的异常   公共的异常类
            throw new ServiceException(ResultCode.AILED_USER_EXISTS);
        }
        String userAccount = account.init();
        sysUser.setUserAccount(userAccount);
        sysUser.setUsername(sysUserSaveDTO.getUsername());
        sysUser.setPassword(BCryptUtils.encryptPassword(sysUserSaveDTO.getPassword()));
        sysUser.setCreateBy(Constants.SYSTEM_USER_ID);
        try {
            return sysUserMapper.insert(sysUser);
        } catch (Exception e) {
            log.error("执行sql-新增管理员出错:" + e.getCause());
        }
        return 0;
    }

    @Override
    public int delete(Long id) {
        User user = userMapper.selectById(id);
        if (user == null) {
            throw new ServiceException(ResultCode.FAILED_USER_NOT_EXISTS);
        }
        return userMapper.deleteById(id);
    }

    @Override
    public List<UserVO> list(UserQueryDTO userQueryDTO) {
        log.info("======userQueryDTO.getPageNum()======" + userQueryDTO.getPageNum());
        log.info("====== userQueryDTO.getPageSize()======" +  userQueryDTO.getPageSize());
        PageHelper.startPage(userQueryDTO.getPageNum(), userQueryDTO.getPageSize());
        try {
            return sysUserMapper.selectUserList(userQueryDTO);
        } catch (Exception e) {
            log.info("执行sql-查询出错:" + e.getCause());
        }
        return new ArrayList<>();
    }

    @Override
    public int updateStatus(UserDTO userDTO) {
        User user = userMapper.selectById(userDTO.getId());
        if (user == null) {
            throw new ServiceException(ResultCode.FAILED_USER_NOT_EXISTS);
        }
        user.setState(userDTO.getStatus());
        // todo 先删除缓存，再更新数据库
        return userMapper.updateById(user);
    }
}
